#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
#include "cgic.h"
#include <mqueue.h>
#include <sys/msg.h>

#define MSG_NAME "/msg_file_vdec"

typedef struct
{
    long mtype;
    int data;
} Message;

Message message;

int cgiMain()
{
    key_t key_msg;
    int msgid;

    key_msg = ftok(MSG_NAME, 'k');

    msgid = msgget(key_msg, 0666 | O_RDWR);
    printf("msgid = %d\n", msgid);

    cgiHeaderContentType("text/html");
    fprintf(cgiOut, "<HTML><HEAD>\n");
    fprintf(cgiOut, "<TITLE>Snap_Picture</TITLE></HEAD>\n");
    fprintf(cgiOut, "<BODY>");
    fprintf(cgiOut, "<H2>MSGQUEUE_INFO = %d</H2>", msgid);
    fprintf(cgiOut, "</BODY>\n");
    fprintf(cgiOut, "</HTML>\n");

    // 向 get.c 发送消息：0
    message.mtype = 1;
    message.data = 1;

    if (msgsnd(msgid, &message, sizeof(int), 0) == -1) //发送抓拍消息
    {
        fprintf(cgiOut, "<H2>ERROR SEND QUEUE</H2>");
        exit(1);
    }
    else
    {
        fprintf(cgiOut, "<H2>SUCCESS SEND QUEUE</H2>");
    }

    return 0;
}
